import CryptoJS from 'crypto-js'

class Tool {
  constructor() {
    console.log('LogHelper ininted');
  }

  /**
   * 获取加密秘钥
   * @private
   */
  _getKey() {
    return CryptoJS.enc.Latin1.parse('whalecloudofkeys')
  }

  /**
   * aes加密
   * @param {string} str
   * @returns {string}
   */
  aesEncrypt(str) {
    // 秘钥
    const key = this._getKey()
    // 进行加密
    const encrypt = CryptoJS.AES.encrypt(str, key, {
      iv: key,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.ZeroPadding
    })
    return encodeURIComponent(encrypt.toString())
  }

  /**
   * aes解密
   * @param {string} str
   * @returns {string}
   */
  aesDecrypt(str) {
    // 秘钥
    const key = this._getKey()
    const bytes = CryptoJS.AES.decrypt(str, key, {
      iv: key
    })
    return bytes.toString(CryptoJS.enc.Utf8);
  }
}
const tool = new Tool();
export default tool
